package cn.wizzer.app.cms.modules.services.impl;

import cn.wizzer.app.cms.modules.models.CmsActivity;
import cn.wizzer.app.cms.modules.services.CmsActivityService;
import cn.wizzer.framework.base.service.BaseServiceImpl;
import com.alibaba.dubbo.config.annotation.Service;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Times;
import org.nutz.plugins.wkcache.annotation.CacheDefaults;
import org.nutz.plugins.wkcache.annotation.CacheRemoveAll;
import org.nutz.plugins.wkcache.annotation.CacheResult;

import java.util.List;

/**
 * @author zl
 * @date 2020/7/1 11:02
 */
@IocBean(args = {"refer:dao"})
@Service(interfaceClass = CmsActivityService.class)
@CacheDefaults(cacheName = "cms_activity")
public class CmsActivityServiceImpl extends BaseServiceImpl<CmsActivity> implements CmsActivityService {
    public CmsActivityServiceImpl(Dao dao) {
        super(dao);
    }

    @Override
    @CacheResult
    public List<CmsActivity> list(String language) {
        String now = Times.format("yyyy-MM-dd", Times.now());
        Cnd cnd = Cnd.where("delFlag", "=", 0)
                .and("disabled","=",0)
                .and("language","=",language)
                .and(Cnd.exps("isLongActivity", "=", 1)
                        .or(Cnd.exps("startDate", ">=", now)
                                .and("endDate", "<=", now)));
        cnd.orderBy("ctAt","DESC");
        return this.query(cnd);
    }

    @Override
    @CacheRemoveAll
    public void del(String id) {
        this.vDelete(id);
    }

    @Override
    @CacheRemoveAll
    public void disable(String id, Integer status) {
        this.update(Chain.make("disabled", status), Cnd.where("id", "=", id));
    }

    @Override
    @CacheRemoveAll
    public void edit(CmsActivity cmsActivity) {
        cmsActivity.setOpAt(1L);
        this.update(cmsActivity);
    }

    @Override
    @CacheRemoveAll
    public void add(CmsActivity cmsActivity) {
        cmsActivity.setOpAt(1L);
        cmsActivity.setCtAt(Times.getTS());
        this.insert(cmsActivity);
    }
}
